/*
 * Attachment[image, audio, video]
 */

import Attachment from 'constants/attachment';
import { CALL_BOOK_API } from '../middlewares';

/**
 * 查询附件列表
 * @param filters
 */
export function fetchAttachList(filters: Record<string, any>) {
  return {
    filters,
    [CALL_BOOK_API]: {
      types: [
        Attachment.FETCH_ATTACHMENT_LIST,
        Attachment.FETCH_ATTACHMENT_LIST_SUCCESS,
        Attachment.FETCH_ATTACHMENT_LIST_FAIL,
      ],
      uri: '/course-api/a/attachments',
      method: 'GET',
      body: filters,
    },
  };
}

export function removeAttachment(attachmentId: number) {
  return {
    attachmentId,
    [CALL_BOOK_API]: {
      types: [Attachment.REMOVE, Attachment.REMOVE_SUCCESS, Attachment.REMOVE_FAIL],
      uri: `/course-api/a/attachments/${attachmentId}`,
      method: 'DELETE',
    },
  };
}

export function updateAttachment(attachmentId: number, data: Record<string, any>) {
  return {
    attachmentId,
    [CALL_BOOK_API]: {
      types: [Attachment.UPDATE, Attachment.UPDATE_SUCCESS, Attachment.UPDATE_FAIL],
      uri: `/course-api/a/attachments/${attachmentId}`,
      method: 'PUT',
      body: data,
    },
  };
}

/**
 * Attachment item checkable 选中状态
 * @param item
 * @param checked
 * @param max 最大选择数量,0表示不限制
 */
export function toggleItemCheck(item: any, checked: boolean, max: number = 0) {
  return {
    type: Attachment.TOGGLE_ITEM_CHECK,
    checked,
    item,
    max,
  };
}

export function initSelectedItems(ownItems: any[]) {
  return {
    type: Attachment.INIT_SELECTED_ITEM,
    ownItems,
  };
}
